﻿using System.Security.Cryptography.X509Certificates;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using AssetManageSystem.Model.AssetManage;
using AssetManageSystem.Model.OrgManage;
using AssetManageSystem.ViewModel.AssetManage.AssetInfoVMs;
using AssetManageSystem.Model;
using AssetManageSystem.Model.Enum;


namespace AssetManageSystem.ViewModel.AssetManage.AssetAllocationVMs
{
    public partial class AssetAllocationVM : BaseCRUDVM<AssetAllocation>
    {
        public List<ComboSelectListItem> AllOldUsers { get; set; }
        public List<ComboSelectListItem> AllOldDeps { get; set; }
        public List<ComboSelectListItem> AllNewUsers { get; set; }
        public List<ComboSelectListItem> AllNewDeps { get; set; }

        public AssetInfoListVM AssetInfoList { get; set; }

        public AssetAllocationVM()
        {
            SetInclude(x => x.OldUser);
            SetInclude(x => x.OldDep);
            SetInclude(x => x.NewUser);
            SetInclude(x => x.NewDep);
            //SetInclude(x => x.AssetId);
        }

        protected override void InitVM()
        {
            AssetInfoList = new AssetInfoListVM();
            AssetInfoList.CopyContext(this);
            //AssetInfoList.SearcherMode = ListVMSearchModeEnum.MasterDetail;
            AssetInfoList.Searcher.Id = Entity.AssetId;

            AllOldUsers = DC.Set<User>().GetSelectListItems(Wtm, y => y.UserName);
            AllOldDeps = DC.Set<Department>().GetSelectListItems(Wtm, y => y.DepName);
            AllNewUsers = DC.Set<User>().GetSelectListItems(Wtm, y => y.UserName);
            AllNewDeps = DC.Set<Department>().GetSelectListItems(Wtm, y => y.DepName);

            Entity.DocNumber = "DB" + Helper.RandomNumber.GetRandom();

        }

        public override void DoAdd()
        {
            var AssetInfos = DC.Set<AssetInfo>().Where(x => x.ID == Entity.AssetId).Select(x => x).ToList().FirstOrDefault();

            if (AssetInfos != null)
            {
                AssetInfos.Status = AsStatusEnum.OnUse;
                AssetInfos.UseDepId = Entity.NewDepId;
                AssetInfos.UsePeopleId = Entity.NewUserId;
                DC.SaveChanges();
            }

            base.DoAdd();
        }

        public override void DoEdit(bool updateAllFields = false)
        {

            base.DoEdit(updateAllFields);
        }

        public override void DoDelete()
        {
            base.DoDelete();
        }
    }
}
